using System.Text;
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using BA.DAL;

namespace BlazeApps.Library.Page
{
    [Serializable()]
    public class PageViewParent : Csla.ReadOnlyListBase<PageViewParent, PageViewChild>
    {
        public enum ViewType
        {
            Preview,
            Live,
            Revision
        }

        private BA.DAL.DBUtility DAL = new BA.DAL.DBUtility();
        private Procedures Procs = new Procedures();

        #region Authorization Rules

        public static bool CanGetObject()
        {
            //TODO: Define CanGetObject permission in PageViewParent
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("PageViewParentViewGroup"))
            //	return true;
            //return false;
        }
        #endregion //Authorization Rules

        #region Factory Methods
        private PageViewParent()
        { /* require use of factory method */
        }

        public static PageViewParent GetPageViewParent(Guid pageid, ViewType pageview)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a PageViewParent");
            return DataPortal.Fetch<PageViewParent>(new FilterCriteria(pageid, pageview));
        }

        public static PageViewParent GetPageViewParent(Guid pageid, ViewType pageview, Int32 revision)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a PageViewParent");
            return DataPortal.Fetch<PageViewParent>(new FilterCriteria(pageid, pageview, revision));
        }

        public static PageViewParent GetPageViewParent(Guid pageid, Guid Templateid, ViewType pageview)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a PageViewParent");
            return DataPortal.Fetch<PageViewParent>(new FilterCriteria(pageid, Templateid, pageview));
        }

        public static PageViewParent GetPageViewParent(Guid pageid, Guid Templateid, ViewType pageview, Int32 revision)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a PageViewParent");
            return DataPortal.Fetch<PageViewParent>(new FilterCriteria(pageid, Templateid, pageview, revision));
        }

        #endregion //Factory Methods

        #region Data Access

        #region Filter Criteria
        [Serializable()]
        private class FilterCriteria
        {
            public Guid pageid;
            public Guid Templateid;
            public ViewType pageview;
            public Int32 revision;

            public FilterCriteria()
            {

            }

            public FilterCriteria(Guid pageid)
            {
                this.pageid = pageid;
            }

            public FilterCriteria(Guid pageid, ViewType pageview)
            {
                this.pageid = pageid;
                this.pageview = pageview;
            }

            public FilterCriteria(Guid pageid, ViewType pageview, Int32 revision)
            {
                this.pageid = pageid;
                this.pageview = pageview;
                this.revision = revision;
            }

            public FilterCriteria(Guid pageid, Guid Templateid, ViewType pageview)
            {
                this.pageid = pageid;
                this.Templateid = Templateid;
                this.pageview = pageview;
            }

            public FilterCriteria(Guid pageid, Guid Templateid, ViewType pageview, Int32 revision)
            {
                this.pageid = pageid;
                this.Templateid = Templateid;
                this.pageview = pageview;
                this.revision = revision;
            }
        }
        #endregion //Filter Criteria

        #region Data Access - Fetch
        private void DataPortal_Fetch(FilterCriteria Criteria)
        {
            RaiseListChangedEvents = false;
            IsReadOnly = false;

            ExecuteFetch(Criteria);

            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }

        private void ExecuteFetch(FilterCriteria Criteria)
        {
            BlazeApps.Library.ConfigSettings Config = new BlazeApps.Library.ConfigSettings();
            BA.DAL.DBUtility DAL = new BA.DAL.DBUtility();
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            if (Criteria.pageview == ViewType.Revision)
            {
                DAL.AddParameter("@pageid", Criteria.pageid);
                DAL.AddParameter("@revision", Criteria.revision);

                using (SafeDataReader dr = new SafeDataReader(DAL.ExecuteReader(Procs.GetPageArchiveDetails.ToString().Trim(), CommandType.Text, ConnectionState.Open)))
                {
                    while (dr.Read())
                        this.Add(PageViewChild.GetPageViewChild(dr));
                }
            }

            if (Criteria.pageview == ViewType.Preview)
            {
                DAL.AddParameter("@pageid", Criteria.pageid);

                using (SafeDataReader dr = new SafeDataReader(DAL.ExecuteReader(Procs.GetPagePreviewDetails.ToString().Trim(), CommandType.Text, ConnectionState.Open)))
                {
                    while (dr.Read())
                        this.Add(PageViewChild.GetPageViewChild(dr));
                }
            }

            if (Criteria.pageview == ViewType.Live)
            {
                DAL.AddParameter("@pageid", Criteria.pageid);

                using (SafeDataReader dr = new SafeDataReader(DAL.ExecuteReader(Procs.GetPageViewDetails.ToString().Trim(), CommandType.Text, ConnectionState.Open)))
                {
                    while (dr.Read())
                        this.Add(PageViewChild.GetPageViewChild(dr));
                }
            }
        }
        #endregion //Data Access - Fetch
        #endregion //Data Access
    }
}
